package com.example.cardgame

import android.annotation.SuppressLint
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.RadioButton
import androidx.core.graphics.drawable.toBitmap

class SecondActivity : AppCompatActivity() {

    private val REQUEST_IMAGE_CAPTURE = 1
    private lateinit var sharedPreferences: SharedPreferences
    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main2)
//        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
//            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
//            insets
//        }
        sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE)

        val takePictureButton: Button = findViewById(R.id.button3)
        val editText1 = findViewById<EditText>(R.id.edittext1)
        val editText2 = findViewById<EditText>(R.id.edittext2)
        val editText3 = findViewById<EditText>(R.id.edittext3)
        val editText4 = findViewById<EditText>(R.id.edittext4)
        val editText5 = findViewById<EditText>(R.id.edittext5)
        val radioButton1 = findViewById<RadioButton>(R.id.radioButton1)
        val radioButton2 = findViewById<RadioButton>(R.id.radioButton2)
        val button2 = findViewById<Button>(R.id.button4)
        val button3 = findViewById<Button>(R.id.button5)
        val imageView = findViewById<ImageView>(R.id.imageView)

        //读取信息
        editText1.setText(sharedPreferences.getString("inputText1", ""))
        editText2.setText(sharedPreferences.getString("inputText2", ""))
        editText3.setText(sharedPreferences.getString("inputText3", ""))
        editText4.setText(sharedPreferences.getString("inputText4", ""))
        editText5.setText(sharedPreferences.getString("inputText5", ""))

        val selectedOption = sharedPreferences.getString("selected_option", "")
        if (selectedOption == radioButton1.text) {
            radioButton1.isChecked = true
        } else if (selectedOption == radioButton2.text) {
            radioButton2.isChecked = true
        }

        //拍照
        takePictureButton.setOnClickListener {
            dispatchTakePictureIntent()
        }

        //保存
        button2.setOnClickListener {
            // 输入文本
            val inputText1 = editText1.text.toString()
            val inputText2 = editText2.text.toString()
            val inputText3 = editText3.text.toString()
            val inputText4 = editText4.text.toString()

            // 保存信息
            with(sharedPreferences.edit()) {
                putString("inputText1", inputText1)
                putString("inputText2", inputText2)
                putString("inputText3", inputText3)
                putString("inputText4", inputText4)

                if (radioButton1.isChecked) {
                    putString("selected_option", radioButton1.text.toString())
                } else if (radioButton2.isChecked) {
                    putString("selected_option", radioButton2.text.toString())
                }

                apply()
            }
            //图片
            val imageBitmap = imageView.drawable?.toBitmap()
            if (imageBitmap != null) {
                intent.putExtra("photo_bitmap", imageBitmap)
            }

            // 跳转到
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)

            //取消
            button3.setOnClickListener {
                finish()
            }
        }
    }

    fun dispatchTakePictureIntent() {
        val takePictureIntent= Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if (intent.resolveActivity(packageManager) !=null){
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
        }
    }
    override fun onActivityResult(requestCode:Int,resultCode:Int,data:Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        val imageview = findViewById<ImageView>(R.id.imageView)
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
            val imageBitmap: Bitmap = data?.extras?.get("data") as Bitmap
            imageview.setImageBitmap(imageBitmap)
        }
    }
}